using System;
using System.Data.Entity.ModelConfiguration;
using System.Data.Common;
using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Xoohoo.Repositories.SqlServerEF.Entities;

namespace Xoohoo.Repositories.SqlServerEF.Mapping
{
	public class RoleMap : EntityTypeConfiguration<Role>
	{
		public RoleMap()
		{
			// Primary Key
			this.HasKey(t => t.RoleID);

			// Properties
			this.Property(t => t.RoleName)
				.IsRequired()
				.HasMaxLength(50);
				
			// Table & Column Mappings
			this.ToTable("Role");
			this.Property(t => t.RoleID).HasColumnName("RoleID");
			this.Property(t => t.RoleName).HasColumnName("RoleName");
			this.Property(t => t.DisplayOrder).HasColumnName("DisplayOrder");
			this.Property(t => t.IsSystem).HasColumnName("IsSystem");

			// Relationships
            this.HasMany(t => t.Permissions)
                    .WithMany(t => t.Roles)
                    .Map(m =>
                    {
                        m.ToTable("RolePermissionRelationShip");
                        m.MapLeftKey("RoleID");
                        m.MapRightKey("PermissionID");
                    });
					
		}
	}
}

